package cn.edu.sgu.www.mhxysy.service.trade.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.trade.TradeRecordDTO;
import cn.edu.sgu.www.mhxysy.entity.trade.TradeRecord;
import cn.edu.sgu.www.mhxysy.mapper.trade.TradeRecordMapper;
import cn.edu.sgu.www.mhxysy.pager.trade.TradeRecordPager;
import cn.edu.sgu.www.mhxysy.service.trade.TradeRecordService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class TradeRecordServiceImpl implements TradeRecordService {

    private final TradeRecordMapper tradeRecordMapper;

    @Autowired
    public TradeRecordServiceImpl(TradeRecordMapper tradeRecordMapper) {
        this.tradeRecordMapper = tradeRecordMapper;
    }

    @Override
    public void insert(TradeRecordDTO tradeRecordDTO) {
        TradeRecord tradeRecord = toEntity(tradeRecordDTO);

        tradeRecord.setId(null);

        tradeRecordMapper.insert(tradeRecord);
    }

    @Override
    public void deleteById(Integer id) {
        tradeRecordMapper.deleteById(id);
    }

    @Override
    public void updateById(TradeRecordDTO tradeRecordDTO) {
        ObjectUtils.requireNonNull(tradeRecordDTO.getId(), CustomMessages.ID_NOT_NULL);

        TradeRecord tradeRecord = toEntity(tradeRecordDTO);

        tradeRecordMapper.updateById(tradeRecord);
    }

    @Override
    public Page<TradeRecord> selectByPage(TradeRecordPager pager) {
        QueryWrapper<TradeRecord> queryWrapper = new QueryWrapper<>();
        Page<TradeRecord> page = Pager.ofPage(pager);

        LocalDateTime from = pager.getFrom();
        LocalDateTime to = pager.getTo();

        if (from != null && to != null) {
            if (from.isBefore(to)) {
                queryWrapper.between("trade_time", from, to);
            }
        } else if (from != null) {
            queryWrapper.ge("trade_time", from);
        } else if (to != null) {
            queryWrapper.le("trade_time", to);
        }

        queryWrapper.eq(
                StringUtils.isNotEmpty(pager.getRoleId()),
                "role_id ", pager.getRoleId()
        );
        queryWrapper.eq(
                pager.getTradeType() != null,
                "trade_type", pager.getTradeType()
        );

        // 得到order by语句
        String statement = Pager.getOrderByStatement(pager);

        if (StringUtils.isNotEmpty(statement)) {
            queryWrapper.last(statement);
        }

        return tradeRecordMapper.selectPage(page, queryWrapper);
    }

    private TradeRecord toEntity(TradeRecordDTO tradeRecordDTO) {
        TradeRecord tradeRecord = new TradeRecord();

        tradeRecord.setId(tradeRecordDTO.getId());
        tradeRecord.setPrice(tradeRecordDTO.getPrice());
        tradeRecord.setRoleId(tradeRecordDTO.getRoleId());
        tradeRecord.setDataId(tradeRecordDTO.getDataId());
        tradeRecord.setTableName(tradeRecordDTO.getTableName());
        tradeRecord.setTradeTime(tradeRecordDTO.getTradeTime());
        tradeRecord.setTradeType(tradeRecordDTO.getTradeType());
        tradeRecord.setCrossSever(tradeRecordDTO.getCrossSever());
        tradeRecord.setDesignated(tradeRecordDTO.getDesignated());

        return tradeRecord;
    }

}